
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.common.entity.UserBalanceRecord;
import com.shop.cloud.mall.admin.service.UserBalanceRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import java.util.Map;

/**
 * 用户余额记录
 *
 * @author
 * @date 2023-11-01 09:50:23
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/userbalancerecord")
@Tag(description = "userbalancerecord", name = "用户余额记录管理")
public class UserBalanceRecordController {

    private final UserBalanceRecordService userBalanceRecordService;

    /**
     * 用户余额记录分页列表
     * @param page 分页对象
     * @param userBalanceRecord 用户余额记录
     * @return
     */
    @Operation(summary = "用户余额记录分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:userbalancerecord:index')")
    public R getPage(Page page, UserBalanceRecord userBalanceRecord) {
        return R.ok(userBalanceRecordService.page(page, Wrappers.query(userBalanceRecord)));
    }

    /**
     * 用户余额记录查询
     * @param id
     * @return R
     */
    @Operation(summary = "用户余额记录查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:userbalancerecord:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(userBalanceRecordService.getById(id));
    }

    /**
     * 用户余额记录删除
     * @param id
     * @return R
     */
    @Operation(summary = "用户余额记录删除")
    @SysLog("删除用户余额记录")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:userbalancerecord:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(userBalanceRecordService.removeById(id));
    }

    /**
     * 通过用户余额记录各类型总金额
     * @param type
     * @param userId
     * @return R
     */
    @Operation(summary = "通过用户余额记录各类型总金额")
    @GetMapping("/sum/{type}/{userid}")
    @PreAuthorize("@ato.hasAuthority('mall:userbalancerecord:index')")
    public R getSum(@PathVariable("type") String type, @PathVariable("userid") String userId) {
        QueryWrapper<UserBalanceRecord> wrapper = new QueryWrapper<>();
        wrapper.select("sum(amount) as sum_amount");
        wrapper.eq("`type`", type);
        wrapper.eq("user_id", userId);
        wrapper.eq("is_pay", CommonConstants.YES);
        Map<String, Object> map = userBalanceRecordService.listMaps(wrapper).get(0);
        if(map != null){
            return R.ok(map.get("sum_amount"));
        }else{
            return R.ok(0);
        }
    }
}
